import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class BorrowRecord {
    public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private Book book;
    private Reader reader;
    private LocalDateTime borrowTime;
    private LocalDateTime returnTime;

    public BorrowRecord(Book book, Reader reader, LocalDateTime borrowTime) {
        this.book = book;
        this.reader = reader;
        this.borrowTime = borrowTime;
        this.returnTime = null;
    }

    public Book getBook() { return book; }
    public Reader getReader() { return reader; }
    public LocalDateTime getBorrowTime() { return borrowTime; }
    public LocalDateTime getReturnTime() { return returnTime; }
    public void setReturnTime(LocalDateTime returnTime) { this.returnTime = returnTime; }

    @Override
    public String toString() {
        String returnTimeStr = (returnTime == null) ? "未还书" : returnTime.format(FORMATTER);
        return "读者：" + reader.getName() + "（" + reader.getCardId() + "），借阅图书：" + book.getTitle() + "（" + book.getIsbn() + "），" +
                "借阅时间：" + borrowTime.format(FORMATTER) + "，还书时间：" + returnTimeStr;
    }

    public String toFileString() {
        String returnTimeStr = (returnTime == null) ? "null" : returnTime.format(FORMATTER);
        return book.getIsbn() + "," + reader.getCardId() + "," + borrowTime.format(FORMATTER) + "," + returnTimeStr;
    }
}

